package com.egoo.ticket.server.utils.activiti;

import com.egoo.ticket.server.pojo.vo.ActiviVo;
import com.egoo.ticket.server.utils.activiti.command.Jump2TargetFlowNodeCommand;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author：victor_tang
 * @Date ：2020/4/17
 * @Description：activit流程引擎通用工具类
 */
public class ActCommonUtils {
	/**
	 * 创建流程引擎对象
	 */
	public static ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	/**
	 * TODO activiti的部署方法
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:33
	 * @param bpmnPath 为资源的相对路径
	 * @param bpmnName 为部署的流程名称
	 * @return: void
	 */
	public static void  deploy(String bpmnPath,String bpmnName){
		RepositoryService repositoryService = processEngine.getRepositoryService();
		String[] strArray = bpmnPath.replaceAll("\\\\","/").split("/");
		String resourceName = strArray[strArray.length-1];
		//读取资源作为一个输入流
		FileInputStream bpmnfileInputStream = null;
		try {
			bpmnfileInputStream = new FileInputStream(bpmnPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// 获取流程定义和部署对象相关的Service，创建部署对象，完成部署
		repositoryService.createDeployment().name(bpmnName)
				.addInputStream(resourceName,bpmnfileInputStream)
				.deploy();
	}

	/**
	 * TODO activiti的启动流程方法
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:32
	 * @param processInstanceKey xml中process的id
	 * @param authenticatedUserId 启动流程人员id
	 * @return: com.egoo.ews.service.vo.ActiviVo
	 */
	public static ActiviVo startProcessInstance(String processInstanceKey,
												String authenticatedUserId) {
		RuntimeService runtimeService = processEngine.getRuntimeService();
		IdentityService identityService = processEngine.getIdentityService();
		TaskService taskService = processEngine.getTaskService();
		// 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
		identityService.setAuthenticatedUserId(authenticatedUserId);
		ProcessInstance pi = runtimeService
				.startProcessInstanceByKey(processInstanceKey);
		Task task = null;
		if (!StringUtils.isEmpty(authenticatedUserId)) {
			task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee(authenticatedUserId).singleResult();
		}else {
			task = taskService.createTaskQuery().processInstanceId(pi.getId()).taskUnassigned().singleResult();

		}
		ActiviVo activiVo = new ActiviVo();
        activiVo.setProcessInstId(pi.getId());
        activiVo.setTaskId(task.getId());
        return  activiVo;
	}

	/**
	 * TODO activiti的任意跳转方法
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:31
	 * @param nodeId 节点Id也就是xml中userTask的Id
	 * @param processInstId 流程实例Id
	 * @return: void
	 */
	public static void arbitraryJump(String nodeId, String processInstId){
		Task task = processEngine.getTaskService().createTaskQuery().processInstanceId(processInstId).active().singleResult();
		Jump2TargetFlowNodeCommand.jump2TargetFlowNode(task.getId(),"_" + nodeId);
	}

	/**
	 * TODO 关闭部署的xml流程
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:31
	 * @param processInstId 流程实例Id
	 * @return: void
	 */
	public static void closeXml(String processInstId){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		RuntimeService runtimeService = processEngine.getRuntimeService();
		RepositoryService repositoryService = processEngine.getRepositoryService();
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstId).singleResult();
		ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId())
				.singleResult();
		repositoryService.deleteDeployment(processDefinition.getDeploymentId(), true);
	}

}
